/**
 * Created by zy on 2016/6/8.
 * 维修记录模板 - 非内检测管理 - 管体缺陷
 */

var pipe_replace = {
    repairReplacePipe: '',
    repairPipeWelding: ''
};

var Template = {

    // 其他修复信息
    getTemplate: function(data){

        var replace = [
            {key:'repairCoat',value:'防腐层修复',node:[
                {name:'isJoint',title:'是否为补口'},
                {name:'environmentalTemperature',title:'环境温度(℃)'},
                {name:'surfaceTemperature',title:'管表温度(℃)'},
                {name:'repairMaterial',title:'修复材料'},
                {name:'repairLength',title:'修复长度(mm)'},
                {name:'repairThickness',title:'厚度(mm)'},
                {name:'edmLeak',title:'电火花检漏电压(kV)'},
                {name:'workload',title:'工作量(㎡)'},
                {name:'materialManufacturer',title:'材料厂商'},
                {name:'createRemark',title:'备注'},
                {name:'att',title:'照片'}
            ]},
            {key:'repairComposites',value:'复合材料补强修复',node:[
                {name:'repairMethod',title:'修复方法'},
                {name:'repairMaterial',title:'修复材料'},
                {name:'repairLength',title:'修复长度(mm)'},
                {name:'repairLayer',title:'修复层数(层)'},
                {name:'environmentalTemperature',title:'环境温度(℃)'},
                {name:'surfaceTemperature',title:'管表温度(℃)'},
                {name:'solidificationTime',title:'固化时间(min)'},
                {name:'hardness',title:'巴氏硬度(HBa)'},
                {name:'materialManufacturer',title:'材料厂商'},
                {name:'createRemark',title:'备注'},
                {name:'att',title:'照片'}
            ]},
            {key:'repairEpoxySleeve',value:'钢质环氧套筒补强修复',node:[
                {name:'socketStructure',title:'套筒结构'},
                {name:'wallThickness',title:'壁厚(mm)'},
                {name:'od',title:'外径(mm)'},
                {name:'material',title:'材质'},
                {name:'repairLength',title:'修复长度(mm)'},
                {name:'environmentalTemperature',title:'环境温度(℃)'},
                {name:'surfaceTemperature',title:'管表温度(℃)'},
                {name:'perfusionProcess',title:'灌注工艺'},
                {name:'workload',title:'工作量(㎡)'},
                {name:'manufacturer',title:'生产厂商'},
                {name:'createRemark',title:'备注'},
                {name:'att',title:'照片'}
            ]},
            {key:'repairABTypeSleeve',value:'A型套筒补强修复',node:[
                {name:'wallThickness',title:'套筒信息(壁厚)'},
                {name:'length',title:'套筒长度(mm)'},
                {name:'environmentalTemperature',title:'环境温度(℃)'},
                {name:'materialType',title:'材质类型'},
                {name:'material',title:'材质'},
                {name:'surfaceTemperature',title:'管表温度(℃)'},
                {name:'weldingGrade',title:'焊材牌号'},
                {name:'weldingMethod',title:'焊接方法'},
                {name:'createRemark',title:'备注'},
                {name:'att',title:'照片'}
            ]},
            {key:'repairOther',value:'其他修复流程',node:[
                {name:'createRemark',title:'备注'},
                {name:'att',title:'照片'}
            ]}
        ];

        var span = '';

        for(var i = 0, len = replace.length; i < len; i++){
            if(data.hasOwnProperty(replace[i].key)){
                for(var j = 0, lenth = replace[i].node.length; j < lenth; j++){
                    if(data[replace[i].key].hasOwnProperty(replace[i].node[j].name)){
                        if(replace[i].node[j].name == 'att' && data[replace[i].key][replace[i].node[j].name].length > 0){
                            span += '<div class="zy-repair-details-head">'+replace[i].node[j].title+'</div>';
                            span += '<div class="zy-repair-img-div">';

                            var $before = $('<div class="zy-repair-img-before"><span class="zy-repair-img-title">修复前</span></div>');
                            var $in = $('<div class="zy-repair-img-in"><span class="zy-repair-img-title">修复中</span></div>');
                            var $after = $('<div class="zy-repair-img-after"><span class="zy-repair-img-title">修复后</span></div>');
                            var $weld = $('<div class="zy-repair-img-weld"><span class="zy-repair-img-title">焊接作业指导书</span></div>');
                            var $examin = $('<div class="zy-repair-img-examin"><span class="zy-repair-img-title">无损检测报告</span></div>');

                            for(var k = 0, lens = data[replace[i].key][replace[i].node[j].name].length; k < lens; k++){
                                //span += '<span class="zy-repair-img-span"><img alt="'+data[replace[i].key][replace[i].node[j].name][k].realName+'" src="'+DyyBase.file_path+data[replace[i].key][replace[i].node[j].name][k].path+'"></span>';
                                switch(data[replace[i].key][replace[i].node[j].name][k].customType){
                                    case 'REPAIR_BEFORE':
                                        $before.append('<span class="zy-repair-img-span"><a target="_blank" href="'+DyyBase.file_path+data[replace[i].key][replace[i].node[j].name][k].path+'"><img alt="'+data[replace[i].key][replace[i].node[j].name][k].realName+'" src="'+DyyBase.file_path+data[replace[i].key][replace[i].node[j].name][k].path+'"></a></span>');
                                        break;
                                    case 'REPAIR_DURING':
                                        $in.append('<span class="zy-repair-img-span"><a target="_blank" href="'+DyyBase.file_path+data[replace[i].key][replace[i].node[j].name][k].path+'"><img alt="'+data[replace[i].key][replace[i].node[j].name][k].realName+'" src="'+DyyBase.file_path+data[replace[i].key][replace[i].node[j].name][k].path+'"></a></span>');
                                        break;
                                    case 'REPAIR_AFTER':
                                        $after.append('<span class="zy-repair-img-span"><a target="_blank" href="'+DyyBase.file_path+data[replace[i].key][replace[i].node[j].name][k].path+'"><img alt="'+data[replace[i].key][replace[i].node[j].name][k].realName+'" src="'+DyyBase.file_path+data[replace[i].key][replace[i].node[j].name][k].path+'"></a></span>');
                                        break;
                                    case 'WELDWORKTYPE':
                                        $weld.append('<span class="zy-repair-img-span"><a target="_blank" href="'+DyyBase.file_path+data[replace[i].key][replace[i].node[j].name][k].path+'"><img alt="'+data[replace[i].key][replace[i].node[j].name][k].realName+'" src="'+DyyBase.file_path+data[replace[i].key][replace[i].node[j].name][k].path+'"></a></span>');
                                        break;
                                    case 'NDTTYPE':
                                        $examin.append('<span class="zy-repair-img-span"><a target="_blank" href="'+DyyBase.file_path+data[replace[i].key][replace[i].node[j].name][k].path+'"><img alt="'+data[replace[i].key][replace[i].node[j].name][k].realName+'" src="'+DyyBase.file_path+data[replace[i].key][replace[i].node[j].name][k].path+'"></a></span>');
                                        break;
                                    default:
                                        break;
                                }
                            }

                            if($before.find('span.zy-repair-img-span').length > 0){
                                span += $before.get(0).outerHTML;
                            }
                            if($in.find('span.zy-repair-img-span').length > 0){
                                span += $in.get(0).outerHTML;
                            }
                            if($after.find('span.zy-repair-img-span').length > 0){
                                span += $after.get(0).outerHTML;
                            }
                            if($weld.find('span.zy-repair-img-span').length > 0){
                                span += $weld.get(0).outerHTML;
                            }
                            if($examin.find('span.zy-repair-img-span').length > 0){
                                span += $examin.get(0).outerHTML;
                            }

                            span += '</div>';
                        }else{
                            if(data[replace[i].key][replace[i].node[j].name] != null){
                                if(replace[i].node[j].name == 'createRemark'){
                                    span += '<div class="zy-details-mark">'+replace[i].node[j].title+'：'+data[replace[i].key][replace[i].node[j].name]+'</div>';
                                }else{
                                    span += '<p>'+replace[i].node[j].title+'：'+data[replace[i].key][replace[i].node[j].name]+'</p>';
                                }
                            }else{
                                span += '<p>'+replace[i].node[j].title+'：-</p>';
                            }
                        }
                    }
                }
            }
        }

        return '<div class="zy-repair-info-body">'+span+'</div>';
    },

    // 换管信息
    getTemplate2: function(data){
        var replace = {key:'repairReplacePipe',value:'换管-焊接',node:[
            {name:'repairReplacePipe',title:'换管信息',node:[
                {call:'startX','head':'起点坐标(x)'},
                {call:'startY','head':'起点坐标(y)'},
                {call:'startZ','head':'起点坐标(z)'},
                {call:'endX','head':'终点坐标(x)'},
                {call:'endY','head':'终点坐标(y)'},
                {call:'endZ','head':'终点坐标(z)'},
                {call:'startMileage','head':'起始里程(m)'},
                {call:'endMileage','head':'终点里程(m)'},
                {call:'lenth','head':'换管长度(m)'},
                {call:'material','head':'材质'},
                {call:'manuPipeType','head':'制管方式'},
                {call:'diameter','head':'管径(mm)'},
                {call:'wallThickness','head':'壁厚(mm)'},
                {call:'coatingMaterial','head':'防腐层材料'},
                {call:'coatingGrade','head':'防腐等级'},
                {call:'createRemark','head':'备注'},
                {call:'imgsReplace','head':'照片'}
            ]},
            {name:'repairPipeWelding',title:'焊接信息',node:[
                {call:'wpsNumber','head':'WPS编号'},
                {call:'ndtType','head':'无损检测类型'},
                {call:'repairind','head':'是否返修'},
                {call:'weldingMaterial','head':'焊接材料'},
                {call:'testDate','head':'检测日期'},
                {call:'testingCompany','head':'检测单位'},
                {call:'repairPipeWeld',head: '焊缝信息',node:[
                    {see:'gwwNumber', say:'环焊缝编号'},
                    {see:'x', say:'采集坐标(X)'},
                    {see:'y', say:'采集坐标(Y)'},
                    {see:'z', say:'采集坐标(Z)'}
                ]},
                {call:'createRemark','head':'备注'},
                {call:'imgsWelding','head':'照片'}
            ]}
        ]};

        var child = replace.node;
        var span = '';

        for(var i = 0, len = child.length; i < len; i++){
            if(data.hasOwnProperty(child[i].name)){
                span += '<div class="zy-repair-details-head '+child[i].name+'">'+child[i].title+'</div>';
                for(var j = 0 , lenth = child[i].node.length; j < lenth; j++){
                    if(data[child[i].name].hasOwnProperty(child[i].node[j].call)){
                        if(child[i].node[j].call == 'imgsReplace' && data[child[i].name][child[i].node[j].call].length > 0){
                            span += '<div class="zy-repair-details-head '+child[i].name+'">'+child[i].node[j].head+'</div>';
                            span += '<div class="zy-repair-img-div '+child[i].name+'">';

                            var $before = $('<div class="zy-repair-img-before"><span class="zy-repair-img-title">修复前</span></div>');
                            var $in = $('<div class="zy-repair-img-in"><span class="zy-repair-img-title">修复中</span></div>');
                            var $after = $('<div class="zy-repair-img-after"><span class="zy-repair-img-title">修复后</span></div>');

                            for(var k = 0, lens = data[child[i].name][child[i].node[j].call].length; k < lens; k++){
                                switch (data[child[i].name][child[i].node[j].call][k].customType){
                                    case 'REPAIR_BEFORE':
                                        //span += '<span class="zy-repair-img-span"><img alt="'+data[child[i].name][child[i].node[j].call][k].realName+'" src="'+DyyBase.file_path+data[child[i].name][child[i].node[j].call][k].path+'"></span>';
                                        $before.append('<span class="zy-repair-img-span"><a target="_blank" href="'+DyyBase.file_path+data[child[i].name][child[i].node[j].call][k].path+'"><img alt="'+data[child[i].name][child[i].node[j].call][k].realName+'" src="'+DyyBase.file_path+data[child[i].name][child[i].node[j].call][k].path+'"></a></span>');
                                        break;
                                    case 'REPAIR_DURING':
                                        $in.append('<span class="zy-repair-img-span"><a target="_blank" href="'+DyyBase.file_path+data[child[i].name][child[i].node[j].call][k].path+'"><img alt="'+data[child[i].name][child[i].node[j].call][k].realName+'" src="'+DyyBase.file_path+data[child[i].name][child[i].node[j].call][k].path+'"></a></span>');
                                        break;
                                    case 'REPAIR_AFTER':
                                        $after.append('<span class="zy-repair-img-span"><a target="_blank" href="'+DyyBase.file_path+data[child[i].name][child[i].node[j].call][k].path+'"><img alt="'+data[child[i].name][child[i].node[j].call][k].realName+'" src="'+DyyBase.file_path+data[child[i].name][child[i].node[j].call][k].path+'"></a></span>');
                                        break;
                                    default:
                                        break;
                                }
                            }

                            if($before.find('span.zy-repair-img-span').length > 0){
                                span += $before.get(0).outerHTML;
                            }
                            if($in.find('span.zy-repair-img-span').length > 0){
                                span += $in.get(0).outerHTML;
                            }
                            if($after.find('span.zy-repair-img-span').length > 0){
                                span += $after.get(0).outerHTML;
                            }

                            span += '</div>';
                        }else if(child[i].node[j].call == 'imgsWelding' && data[child[i].name][child[i].node[j].call].length > 0){
                            span += '<div class="zy-repair-details-head '+child[i].name+'">'+child[i].node[j].head+'</div>';
                            span += '<div class="zy-repair-img-div '+child[i].name+'"><span class="zy-repair-img-title">焊接照片</span>';
                            for(var l = 0, lenths = data[child[i].name][child[i].node[j].call].length; l < lenths; l++){
                                span += '<span class="zy-repair-img-span"><a target="_blank" href="'+DyyBase.file_path+data[child[i].name][child[i].node[j].call][l].path+'"><img alt="'+data[child[i].name][child[i].node[j].call][l].realName+'" src="'+DyyBase.file_path+data[child[i].name][child[i].node[j].call][l].path+'"></a></span>';
                            }
                            span += '</div>';
                        }else if(child[i].node[j].call == 'repairPipeWeld'){
                            for(var m = 0, length = data[child[i].name][child[i].node[j].call].length; m < length; m++){
                                for(var o = 0, lengths = child[i].node[j].node.length; o < lengths; o++){
                                    if(data[child[i].name][child[i].node[j].call][m].hasOwnProperty(child[i].node[j].node[o].see)){
                                        span += '<p class="'+child[i].name+'">'+child[i].node[j].node[o].say+'：'+data[child[i].name][child[i].node[j].call][m][child[i].node[j].node[o].see]+'</p>';
                                    }
                                }
                            }
                        }else{
                            if(data[child[i].name][child[i].node[j].call] != null){
                                if(child[i].node[j].call == 'createRemark'){
                                    span += '<div class="zy-details-mark '+child[i].name+'">'+child[i].node[j].head+'：'+data[child[i].name][child[i].node[j].call]+'</div>';
                                }else{
                                    span += '<p class="'+child[i].name+'">'+child[i].node[j].head+'：'+data[child[i].name][child[i].node[j].call]+'</p>';
                                }
                            }else{
                                span += '<p class="'+child[i].name+'">'+child[i].node[j].head+'：-</p>';
                            }
                        }
                    }
                }
            }
        }

        var temp_div = $('<div></div>');
        temp_div.append(span);

        pipe_replace.repairReplacePipe = temp_div.find('.repairReplacePipe');
        pipe_replace.repairPipeWelding = temp_div.find('.repairPipeWelding');

        return pipe_replace.repairReplacePipe;
    }
};
